<?php

namespace common\models\base;

use Yii;

/**
 * This is the model class for table "{{%car_gps_order}}".
 *
 * @property integer $id
 * @property integer $guarantee_company_car_id
 * @property integer $guarantee_company_id
 * @property string $gps_serial_number
 * @property integer $status
 * @property integer $created_at
 * @property integer $created_by
 * @property integer $updated_at
 * @property integer $updated_by
 * @property string $data
 *
 * @property User $createdBy
 * @property User $updatedBy
 * @property GuaranteeCompany $guaranteeCompany
 * @property CarGpsOrderStatusUpdateLog[] $carGpsOrderStatusUpdateLogs
 */
class CarGpsOrder extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%car_gps_order}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['guarantee_company_car_id', 'guarantee_company_id', 'gps_serial_number', 'status', 'created_at', 'created_by', 'updated_at', 'updated_by'], 'required'],
            [['guarantee_company_car_id', 'guarantee_company_id', 'status', 'created_at', 'created_by', 'updated_at', 'updated_by'], 'integer'],
            [['data'], 'string'],
            [['gps_serial_number'], 'string', 'max' => 50],
            [['gps_serial_number'], 'unique'],
            [['guarantee_company_car_id', 'guarantee_company_id'], 'unique', 'targetAttribute' => ['guarantee_company_car_id', 'guarantee_company_id'], 'message' => 'The combination of Guarantee Company Car ID and Guarantee Company ID has already been taken.'],
            [['created_by'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['created_by' => 'id']],
            [['updated_by'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['updated_by' => 'id']],
            [['guarantee_company_id'], 'exist', 'skipOnError' => true, 'targetClass' => GuaranteeCompany::className(), 'targetAttribute' => ['guarantee_company_id' => 'id']],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'guarantee_company_car_id' => 'Guarantee Company Car ID',
            'guarantee_company_id' => 'Guarantee Company ID',
            'gps_serial_number' => 'Gps Serial Number',
            'status' => 'Status',
            'created_at' => 'Created At',
            'created_by' => 'Created By',
            'updated_at' => 'Updated At',
            'updated_by' => 'Updated By',
            'data' => 'Data',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCreatedBy()
    {
        return $this->hasOne(User::className(), ['id' => 'created_by']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUpdatedBy()
    {
        return $this->hasOne(User::className(), ['id' => 'updated_by']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getGuaranteeCompany()
    {
        return $this->hasOne(GuaranteeCompany::className(), ['id' => 'guarantee_company_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCarGpsOrderStatusUpdateLogs()
    {
        return $this->hasMany(CarGpsOrderStatusUpdateLog::className(), ['order_id' => 'id']);
    }
}
